/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2020-01-02
 * Time: 19:41
 */
#include "PlantsDao.h"

bool PlantsDao::check(QString plantsName) {
    bool flag = false;
    DBHelper::conn();
    QSqlQuery query;
    QString sql = QString("select name from plantsinfo.plants ;");
    int success = query.exec(sql);
    if (!success) {
        flag = false;  //失败
    } else {
        while (query.next()) {
            if (query.value(0).toString() == plantsName) {
                flag = true;
            }
        }
    }
    return flag;
}

QString PlantsDao::getAll(QString plantsName) {
    QString info;
    DBHelper::conn();
    QSqlQuery query;
    QString sql = QString("select name,base,local,purpose from plants where name='%1'").arg(plantsName);
    int success = query.exec(sql);
    if (!success) {
        qDebug()<<"执行不成功";
    } else {
        query.next();
        info.append("名称,,,,,,,,,,,,,,,");
        info.append(query.value(0).toString());
        info.append("。。。。。。。。。。。。基本信息,,,,,,,,,,,,,,,");
        info.append(query.value(1).toString());
        info.append("。。。。。。。。。。。。产地,,,,,,,,,,,,,,,");
        info.append(query.value(2).toString());
        info.append("。。。。。。。。。。。。。用途,,,,,,,,,,,,,,,");
        info.append(query.value(3).toString());
    }
    return info;
}

QString PlantsDao::getQrPath(QString plantsName) {
    DBHelper::conn();
    QSqlQuery query;
    QString sql = QString("select qrimgpath from plants where name='%1'").arg(plantsName);
    int success = query.exec(sql);
    if (!success) {
        qDebug()<<"执行不成功";
    } else {
        while (query.next()) {
            if (query.value(0).toString() != NULL) {
                return query.value(0).toString();
            }
        }
    }
    return "查询失败";
}
